function ret=Gl1(G,l)
 if(l==1)
  ret={};
  for g = G
   g=g{1};
   ret{end+1}={g};
  endfor
  return;
 endif
 
 ret={};
 prev= Gl1(G,l-1);
 n=1;
 
 for gp = G
  gp=gp{1};
  for g = prev
   g=g{1};
   checkPrefix=g(1:min(7,length(g)));
   if(checkTrivial(gp,checkPrefix)==true)
    continue;
   endif
   ret{n}{1}=gp;
   for j =1 : l-1
    ret{n}{end+1}=g{j};
   endfor
   n=n+1;
  endfor 
 endfor
endfunction

function ret=checkTrivial(g,prefix)
 U=g;
 for j =1:length(prefix)
  U=U*prefix{j};
  if (distance(U,eye(2))<1e-10) | (distance(U,-eye(2))<1e-10)
   ret=true;
   return;
  endif 
 endfor
 ret=false;
endfunction

function [ret1,ret2]=push_stack(x,ele)
 if(isTrivial(x.stack,ele)==true)
  ret1=0;
  ret2=x;
  return;
 endif
 x.stack{end+1}=ele;
 x.tmpsize=x.tmpsize+1;
 x.mat=x.mat*ele;
 ret1=1;
 ret2=x;
endfunction

function ret=isTrivial(list,ele)
 if(length(list)==0)
  ret=false;
  return;
 endif 
 if(list{end}==ele')
  ret=true;
  return;
 endif
 i=0;
 U=ele;
 while i <min(8,length(list))
  U=U*list{end-i};
  if (distance(U,eye(2))<1e-10) | (distance(U,-eye(2))<1e-10)
   ret=true;
  return;
  endif 
  i=i+1;
 endwhile
 ret=false;
endfunction

